package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.common.BizException;
import com.yuelao.yuelao_backend.common.ErrorCode;
import com.yuelao.yuelao_backend.dto.UserDTO;
import com.yuelao.yuelao_backend.dto.request.UpdateUserRequest;
import com.yuelao.yuelao_backend.service.UserService;
import com.yuelao.yuelao_backend.util.UserContextUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 用户Controller
 * 重构说明：使用构造函数注入，适配新的UserService
 */
@RestController
@RequestMapping("/user")
@Slf4j
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;
    private final com.yuelao.yuelao_backend.service.UserLikeService userLikeService;

    /**
     * 获取指定用户详细信息
     * GET /user/{userId}
     * @param userId 用户ID
     * @return 用户信息
     */
    @GetMapping("/{userId}")
    public ApiResponse<UserDTO> getUserInfo(@PathVariable Long userId) {
        if (userId == null || userId <= 0) {
            throw new BizException(ErrorCode.PARAMS_ERROR, "用户ID无效");
        }
        UserDTO userDTO = userService.getUserInfo(userId);
        return ApiResponse.ok(userDTO);
    }

    /**
     * 获取当前登录用户信息
     * GET /user/current
     * @return 当前用户信息
     */
    @GetMapping("/current")
    public ApiResponse<UserDTO> getCurrentUser() {
        // 使用工具类获取当前用户ID
        Long userId = UserContextUtil.getCurrentUserId();
        UserDTO userDTO = userService.getCurrentUser(userId);
        return ApiResponse.ok(userDTO);
    }

    /**
     * 更新用户信息
     * PUT /user/update
     * @param request 更新请求体
     * @return 是否成功
     */
    @PutMapping("/update")
    public ApiResponse<Boolean> updateUserInfo(@RequestBody UpdateUserRequest request) {
        // 使用工具类获取当前用户ID
        Long userId = UserContextUtil.getCurrentUserId();
        boolean result = userService.updateUserInfo(userId, request);
        return ApiResponse.ok(result);
    }

}